package com.fortune.controller;

import com.fortune.base.BaseController;
import com.fortune.dto.excution.CReturnDepositExcution;
import com.fortune.dto.excution.ReturnDepositExcution;
import com.fortune.dto.in.cReturnDeposit.CReturnAddDepositDto;
import com.fortune.dto.in.cReturnDeposit.CReturnDepositEditDto;
import com.fortune.dto.in.returnDeposit.EditDto;
import com.fortune.dto.in.returnDeposit.ReturnDepositEditDto;
import com.fortune.enums.CReturnDepositEnum;
import com.fortune.enums.ReturnDepositEnum;
import com.fortune.service.CReturnDepositService;
import com.fortune.service.ReturnDepositService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.cReturnDeposit.*;
import com.fortune.vo.returnDeposit.DepositProductDetails;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.List;

@Api(value = "wechat/cReturnDeposit", description = "客户退押金工单", tags = {"微信小程序（客户）：退押金工单相关"})
@RestController
@RequestMapping("wechat/cReturnDeposit")
@Validated
public class CReturnDepositController extends BaseController{
	@Autowired
	private CReturnDepositService service;
	@Autowired
	private ReturnDepositService returnDepositService;

	@ApiOperation(value = "退押金工单列表", nickname = "cReturnDepositList", notes = "", tags = {"@安童"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="type", value="0未处理，1已批准，2已发货，3已收货",paramType="query",dataType="int",allowableValues="0,1,2,3"),
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "products",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<CReturnDeposit>> products(Integer type) {
		String did = getDId();
		String id = getUserId();
		List<CReturnDeposit> list = service.findCReturnDepositList(type,did,id);
		if(list.size() == 0){
			return new CReturnDepositExcution<List<CReturnDeposit>>(list, CReturnDepositEnum.FIND_RESULT_AIR);
		}else{
			for(CReturnDeposit cReturnDeposit:list){
				BigDecimal aa = new BigDecimal(0);
			 	List<RefundDetails> lll = service.findCReturnProList(cReturnDeposit.getId());
			 		for(RefundDetails refundDetails : lll){
			 			aa =aa.add(refundDetails.getDepositPrice());
					}
				cReturnDeposit.setRefundDetails(lll);
				cReturnDeposit.setTotalPrice(aa);
			}
		}
		return new CReturnDepositExcution<List<CReturnDeposit>>(list, CReturnDepositEnum.FIND_SUCCESS);
	}


	@ApiOperation(value = "退押金工单详情", nickname = "driverReturnDepositDetail", notes = "", tags = {"@安童"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="id", value="订单id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@GetMapping(value = "detail",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<CReturnDepositDetail> detail(@Validated @NotBlank(message="订单id不能为空") String id,
														@Validated @NotBlank(message="客户id不能为空") String clientId) {
		String did = getDId();
		CReturnDepositDetail cReturnDepositDetail = service.findCReturnDepositDetail(id,clientId,did);
		cReturnDepositDetail.setCreateTime(cReturnDepositDetail.getCreateTime().substring(0,19));
		return new CReturnDepositExcution<CReturnDepositDetail>(cReturnDepositDetail,CReturnDepositEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "删除工单", nickname = "cReturnDepositDelete", notes = "", tags = {"@安童"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="工单id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@NotBlank(message="工单id不能为空") String id){
		String did = getDId();
		String cid = getUserId();
		int num = service.deleteReturnDeposit(id,did,cid);
		if(num > 0){
			return new CReturnDepositExcution<Void>(null,CReturnDepositEnum.DELETE_SUCCESS);
		}
		return new CReturnDepositExcution<Void>(null,CReturnDepositEnum.DELETE_FALSE);
	}
	
	@ApiOperation(value = "申请退押金", nickname = "cReturnDepositApply", notes = "", tags = {"@安童"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "apply",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> apply(@Validated @RequestBody CReturnAddDepositDto dto){
		String did = dto.getDid();
		String cid = getUserId();
		String id = UUIDUtil.get32UUID();
		int num = service.apply(dto,did,cid,id);
		if(num > 0){
			return new CReturnDepositExcution<Void>(null,CReturnDepositEnum.ADD_SUCCESS);
		}else{
			return new CReturnDepositExcution<Void>(null,CReturnDepositEnum.ADD_ERROR);
		}
	}
	
	@ApiOperation(value = "退押金工单概览", nickname = "cReturnDepositListOverview", notes = "", tags = {"@安童"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "overview",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<VReturnDepositOverview> overview() {
		String did = getDId();
		String id = getUserId();
		VReturnDepositOverview vReturnDepositOverview = service.findVReturnDepositOverview(did,id);
		return new CReturnDepositExcution<VReturnDepositOverview>(vReturnDepositOverview,CReturnDepositEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "申请退押金工单商品列表", nickname = "depositProductDetails", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true),
			@ApiImplicitParam(name="did", value="经销商id",paramType="query",dataType="String")
	})
	@GetMapping(value = "productDetails",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DepositProductDetails>> productDetails(@Validated @NotBlank(message="经销商id不能为空") String did) {
		String clientId = getUserId();
		List<DepositProductDetails> findDepositProductDetails = returnDepositService.findDepositProductDetails(did, clientId);
		if(findDepositProductDetails == null){
			return new ReturnDepositExcution<List<DepositProductDetails>>(null, ReturnDepositEnum.FIND_ERROR);
		}
		return new ReturnDepositExcution<List<DepositProductDetails>>(findDepositProductDetails, ReturnDepositEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "编辑工单", nickname = "returnDepositEdit", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PutMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody CReturnDepositEditDto cReturnDepositEditDto) {
		String did = cReturnDepositEditDto.getDid();
		String returnDepositId = cReturnDepositEditDto.getReturnDepositId();
		String clientId = getUserId();
		int delCReturnDeposit = service.delCReturnDeposit(did,clientId, returnDepositId);
		if(delCReturnDeposit < 1){
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_ERROR);
		}
		List<EditDto> editDtos = cReturnDepositEditDto.getEditDtos();
		for(int i=0; i<editDtos.size(); i++){
			EditDto editDto = editDtos.get(i);
			int orderNum = editDto.getOrderNum();
			String mid = editDto.getMid();
			String productId = editDto.getProductId();
			int productCount = editDto.getProductCount();
			BigDecimal depositPrice = editDto.getDepositPrice();
			int addReturnDepositDetails = returnDepositService.addReturnDepositDetails(did, clientId, returnDepositId, orderNum, mid, productId, productCount, depositPrice);
			if (addReturnDepositDetails < 1) {
				return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_ERROR);
			}
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_SUCCESS);
	}

	@ApiOperation(value = "新增退押金工单经销商列表", nickname = "dealerList", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PutMapping(value = "dealerList",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DealerList>> dealerList() {
		String userId = getUserId();
		List<DealerList> findDealerList = returnDepositService.findDealerList(userId);
		return new ReturnDepositExcution<List<DealerList>>(findDealerList, ReturnDepositEnum.FIND_SUCCESS);
	}
}
